package com.hcc.gateway.ops.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hcc.gateway.common.domain.RespBody;
import com.hcc.gateway.common.utils.HttpClientUtils;
import com.hcc.gateway.domain.enums.ContentTypeEnum;
import com.hcc.gateway.domain.resp.ApiTestResp;
import com.hcc.gateway.domain.vo.ApiTestParamVo;
import com.hcc.gateway.domain.req.ApiTestReq;
import com.hcc.gateway.domain.resp.RestResp;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ApiTestController
 *
 * @author hushengjun
 * @date 2022/1/22
 */
@RestController
@RequestMapping("/apiManage")
public class ApiTestController {

    @PostMapping("/testApi")
    public RestResp<ApiTestResp> testApi(@RequestBody ApiTestReq apiTestReq) {
        Map<String, String> headers = new HashMap<>();
        String contentType = null;
        for (ApiTestParamVo paramReq : apiTestReq.getHeaders()) {
            if (!paramReq.getEnable() || StrUtil.isEmpty(paramReq.getName())) {
                continue;
            }
            if ("content-type".equalsIgnoreCase(paramReq.getName())) {
                contentType = paramReq.getValue();
            }
            headers.put(paramReq.getName(), paramReq.getValue());
        }

        Object paramObj;
        if (ContentTypeEnum.FORM_URLENCODED.getCode().equals(contentType)) {
            Map<String, Object> formData = new HashMap<>();
            for (ApiTestParamVo paramReq : apiTestReq.getFormData()) {
                if (!paramReq.getEnable() || StrUtil.isEmpty(paramReq.getName())) {
                    continue;
                }
                formData.put(paramReq.getName(), paramReq.getValue());
            }
            paramObj = formData;
        } else {
            paramObj = apiTestReq.getStringBody();
        }
        RespBody respBody = HttpClientUtils.method(apiTestReq.getUrl(), apiTestReq.getHttpMethod(), headers, contentType,
                paramObj);
        ApiTestResp resp = new ApiTestResp();
        resp.setStatus(respBody.getCode());
        resp.setMsg(respBody.getMessage());
        resp.setContentType(respBody.getContentType());
        resp.setStringBody(respBody.getStringBody());
        if (MapUtil.isNotEmpty(respBody.getHeaders())) {
            List<ApiTestParamVo> respHeaders = respBody.getHeaders().entrySet().stream()
                    .map(entry -> {
                        ApiTestParamVo apiTestParamVo = new ApiTestParamVo();
                        apiTestParamVo.setName(entry.getKey());
                        apiTestParamVo.setValue(String.join(";", entry.getValue()));

                        return apiTestParamVo;
                    })
                    .collect(Collectors.toList());
            resp.setHeaders(respHeaders);
        }

        return RestResp.success(resp);
    }

}
